#!/usr/bin/python

import os
import sys
if 'PYERECTOR_PATH' in os.environ and \
        os.path.isdir(os.environ['PYERECTOR_PATH']):
    for i, p in enumerate(sys.path):
        if p.find('pyerector') != -1:
            sys.path[i] = os.environ['PYERECTOR_PATH']
import release
from pyerector import *

#from pyerector.tasks import Symlink
# try to use ../build/pyerector-{relno}/pyerector
bname = '%s-%s' % (release.product, release.number)
builddir = os.path.join(os.pardir, 'build', bname, 'pyerector')
if os.path.isdir(builddir):
    libdir = os.path.realpath(builddir)
else:
    import pyerector
    libdir = os.path.dirname(pyerector.__file__)
    del pyerector


class Clean(Target):
    def run(self):
        bdir = V['basedir']
        for sdir in os.listdir(bdir):
            fname = normjoin(bdir, sdir, 'pyerector')
            if os.path.islink(fname):
                Remove()(fname)

class Init(Target):
    def run(self):
        bdir = V['basedir']
        for sdirname in os.listdir(bdir):
            sdir = normjoin(bdir, sdirname)
            fname = normjoin(sdir, 'pyerector')
            if os.path.isdir(sdir) and \
                    not os.path.islink(fname):
                os.symlink(libdir, fname)
                #Symlink(fname, dest=libdir)

class Regression(Target):
    dependencies = (Init,)
    """Regression testing."""
    def run(self):
        import os
        subtasks = []
        for fname in os.listdir(V['basedir']):
            sdir = self.join(fname)
            if os.path.isdir(sdir) and \
               os.path.isfile(os.path.join(sdir, 'pyerect')):
                subtasks.append(
                    Echo('Calling %s/pyerect', sdir)
                )
                subtasks.append(
                    SubPyErector('all', wdir=sdir,
                        env={'PYTHONPATH': libdir})
                )
        tasks = Parallel(*tuple(subtasks))
        if callable(tasks):
            tasks()
        else:
            tasks.run(self, None, None, None)

Test.dependencies = ()
All.dependencies = (Clean, Regression,)
Default.dependencies = (Regression,)

PyErector()
